package com.lx;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 文件夹操作
 *
 * @author 段誉
 * @create 2019-03-26 22:12
 */
public class FileCreate {
  private static final String PATH1 = "path1";
  private static final String PATH2 = "path2/path2";
  private static final String PATH3 = "path3";
  private static final String PATH4 = "path4/path4";

  /**
   * 创建文件夹
   */
  public static void createFolder() {
    File file1 = new File(PATH1);
    if (!file1.exists()) {
      System.out.println("path1创建文件结果" + file1.mkdir());
    }

    File file2 = new File(PATH2);
    if (!file2.exists()) {
      System.out.println("path2创建文件结果" + file2.mkdir());
    }

    File file3 = new File(PATH3);
    if (!file3.exists()) {
      System.out.println("path3创建文件结果" + file3.mkdirs());
    }

    File file4 = new File(PATH4);
    if (!file4.exists()) {
      System.out.println("path4创建文件结果" + file4.mkdirs());
    }
  }

  /**
   * 创建文件
   */
  public static void createFile() {
    File file = new File("text.txt");
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      //往文件中写入数据
      FileWriter fileWriter = new FileWriter(file);
      //使用print方式写入数据
      PrintWriter printWriter = new PrintWriter(fileWriter);
      printWriter.println("测试数据");
      printWriter.close();
      fileWriter.close();
    } catch (IOException e) {
      System.out.println("创建文件时出错");
    }
  }

  /**
   * 删除文件
   */
  public static void deleteFile() {
    File file = new File("text.txt");
    file.delete();
  }

  /**
   * 删除文件夹下所有文件
   */
  public static void deleteFolderUnderAllFile() {
    File file = new File("path4");
    //判断文件是否为目录
    if (file.isDirectory()) {
      //获取当前文件夹下的所有文件
      File[] files = file.listFiles();
      for (int i = 0, j = files.length; i < j; i++) {
        files[i].delete();
      }
    }
  }

  public static void main(String[] args) {
    deleteFile();
  }
}
